package chenji.dailykit.ItemEditor;

import java.util.HashMap;

import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

/**
 * @author ChenJi Copyright (c) 2016 ChenJi. All rights reserved.
 */
public class ItemEditorManager {
	private final HashMap<Player, ItemEditor> editorlist = new HashMap<Player, ItemEditor>();
	private final ItemEditorEventManager IEEM = new ItemEditorEventManager();

	public ItemEditorEventManager getItemEditorEventManager() {
		return this.IEEM;
	}

	public void openEditor(Player p, ItemStack item) {
		if (this.editorlist.containsKey(p)) {
			this.removeEditor(p);

		}
		ItemEditor ie = new ItemEditor(p, item);
		this.editorlist.put(p, ie);
		ie.openInventory();

	}

	public void removeEditor(Player p) {
		if (this.editorlist.containsKey(p)) {
			this.editorlist.get(p).getInventory().clear();
			p.closeInventory();
			this.editorlist.remove(p);
			p.sendMessage("§c你清除了你目前的物品编辑器。");
		}
	}

	public boolean setPlayerChat(Player p, String m) {
		if (!(this.editorlist.containsKey(p))) {
			return false;
		}
		return this.editorlist.get(p).setChat(m);

	}

	public void setItem(Player p, ItemStack i) {
		if (this.editorlist.containsKey(p)) {
			this.editorlist.get(p).setItem(i);
		}
	}

	public ItemStack getItem(Player p) {
		if (this.editorlist.containsKey(p)) {
			return this.editorlist.get(p).getItem();
		} else {
			return null;
		}
	}

	public ItemEditor getItemEditor(Player p) {
		if (!(this.editorlist.containsKey(p))) {
			return null;
		} else {
			return this.editorlist.get(p);
		}
	}

	public boolean containItemEditor(Player p) {
		return this.editorlist.containsKey(p);
	}

	public boolean clickInventory(Inventory inventory, Player p, int slot) {
		ItemEditor i = this.editorlist.get(p);
		if (i == null) {
			return false;
		}
		if (!(i.getInventory().equals(inventory))) {
			return false;
		}
		i.clickInventory(p, slot);
		return true;
	}

	public boolean openEditorInventory(Player player) {
		if (this.editorlist.containsKey(player)) {
			this.editorlist.get(player).openInventory();
			return true;
		}
		return false;

	}

}
